package com.arges.controller;

import cn.hutool.core.collection.ListUtil;
import com.arges.pojo.Book;
import com.arges.pojo.Result;
import com.arges.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 新增
     *
     * @param book
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Book book) {
        bookService.save(book);
        return Result.success("添加成功!");
    }

    /**
     * 修改
     *
     * @param book
     */
    @PutMapping
    public Result update(@RequestBody Book book) {
        bookService.update(book);
        return Result.success("修改成功!");
    }

    /**
     * 根据id修改
     *
     * @param id
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        Book book = new Book();
        book.setId(id);
        bookService.delete(book);
        return Result.success("修改成功!");
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Book book = bookService.getById(id);
        return Result.success(book);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    public Result getAll() {
        String hql = "from Book";
        List<Book> books = bookService.findAll(hql);
        return Result.success(books);
    }


    public static void main(String[] args) {

        Integer[] arr = {};
        ArrayList<Integer> list = ListUtil.toList(arr);

        if(CollectionUtils.isEmpty(list)){
            list.add(1);
        }
//        list.add(1);
//        list.add(2);
        if (list.contains(1) && list.contains(2)){
            System.out.println("111");
        }else if (list.contains(2)){
            System.out.println("222");
        }else if (list.contains(1)){
            System.out.println("333");
        }

    }
}